/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminServlet;

import Business.Customer;
import Business.LineItem;
import Business.Order;
import Business.Product;
import Model.DAO;
import com.sun.xml.internal.ws.message.source.PayloadSourceMessage;
import java.io.IOException;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Nguyen Hung
 */
public class ViewOrderServlet extends HttpServlet {
   
   
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String url = "";
        HttpSession session = request.getSession();


        try {
            Connection connection = DAO.getConnection();
            String sql = "select * from shoppingonline.order";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet set = ps.executeQuery();
            ArrayList<Order> orders = new ArrayList<Order>();
            Order order;
            
            while(set.next()){
                String orderId = set.getString("idOrder");
                Date date  = set.getDate("date");
                int customerId = set.getInt("customerId");
                String note = set.getString("note");
                String paytype = set.getString("paytype");
                
                String state = set.getString("state");
                sql = "select * from customer where customerId = ?";
                PreparedStatement ps2 = connection.prepareStatement(sql);
                ps2.setInt(1, customerId);
                ResultSet customerSet = ps2.executeQuery();
                String name, email, address, phoneNumber;
                Customer customer = null;
                while(customerSet.next()){
                    name = customerSet.getString("name");
                    email = customerSet.getString("email");
                    address  = customerSet.getString("address");
                    phoneNumber = customerSet.getString("phoneNumber");
                    
                    customer = new Customer(name, email, address, phoneNumber);

                }
                sql = "select * from orderline where idorder = ?";
                PreparedStatement ps3 = connection.prepareStatement(sql);
                ps3.setString(1, orderId);
                ResultSet orderLineSet = ps3.executeQuery();
                String productCode;
                int quantity;
                LineItem lineItem;
                ArrayList<LineItem> items = new ArrayList<LineItem>();
                while(orderLineSet.next()){
                    productCode = orderLineSet.getString("productCode");
                    quantity = orderLineSet.getInt("quantity");
                    Product product = new Product();
                    product.setProductCode(productCode);
                    product.load();
                    lineItem = new LineItem(product, quantity);
                    items.add(lineItem);

                }

                order = new Order(customer, date, note, paytype, state);
                order.setLineItems(items);
                order.setIdOrder(orderId);
                System.out.println(items.toString());
                orders.add(order);
            }
            session.setAttribute("orders", orders);
            url = "/admin/viewAllOrders.jsp";



        } catch (SQLException ex) {
            Logger.getLogger(ViewOrderServlet.class.getName()).log(Level.SEVERE, null, ex);
            url="/error.jsp";
        }
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
        dispatcher.forward(request, response);
     
    }
   
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

  
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
